<?php
/* 
 *采购计划
 */
class BuyAction extends TransAction {
    public function _initialize() {
            parent::_initialize();
            $this->assign('useAjax',1);
        }
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            //$map['name'] = array('like',"%".$key."%");//产品名称
            $map['buy_no'] = array('like',"%".$key."%");
            //$map['tm'] = array('like',"%".$key."%");//条码
            /* exact filter
            $map['Warehouse.name'] = array('like',"%".$key."%");//仓库
            $map['Customer.name'] = array('like',"%".$key."%");//customer
            $map['number'] = array('eq',$key);
            //$map['minstock'] = array('like',"%".$key."%");*/
            //$map['_logic'] = "or";
        }
        $sid = $_REQUEST['supplier_id'];
        if(!empty($sid))
            $map['supplier_id'] = $sid;
    }

    public function _before_add(){
        parent::_before_add();
        $this->assign('suppliers',getModelIdName('Supplier','sort'));
    }
   
    

    public function _before_edit() {
        parent::_before_edit();
        $buy_details = M('Buy_detail')->alias('e')->
                            join(C('DB_PREFIX').'product p on e.product_id = p.id')->
                            where('buy_id = '.$_GET['id'] )->field('e.*,product_id,p.name,p.tm,p.unit_id')->findAll();
        $this->assign('buy_details',$buy_details); 
        $enter_details = M('Enter_detail')->alias('e')->
                            join(C('DB_PREFIX').'product p on e.product_id = p.id')->
                            where('buy_id = '.$_GET['id'] )->field('e.*,product_id,p.name,p.tm,p.unit_id')->findAll();
        $this->assign('enter_details',$enter_details); 
    }

    //控制重复插入
    public function _before_insert(){
        $data = $_POST;
        $count_products = count($_POST['enter_price']);
        $total = 0 ;
        for( $i = 0; $i < $count_products; $i++ ){
           $total += $data['enter_price'][$i]*$data['number'][$i];
        }

        $exist_map = array(
          'create_time' => array('gt',time() - 5*60 ),
          'supplier_id' => $_REQUEST['supplier_id'],
          'total' => $total,
        );
        $exist_buy = M('Buy')->where($exist_map)->getField('id');
        if($exist_buy)
           $this->error('请勿重复添加,如果不是重复添加的计划,请5分钟后再操作!'); 
    }

    //批量导入库--采购
    public function fastadd(){
        $this->assign('users',getModelIdName('User'));
        
        $this->assign('suppliers',getModelIdName('Supplier','sort'));
        $this->assign("warehouses", getModelIdName('Warehouse'));
        $this->display();
    }

    //import enter product detail
    public function import(){
        $sheetData = A('Import')->getExcel();
        $Product = M('Product');
        $product_str = "";
        $i = 1; //count import record
        foreach ($sheetData as $key => $value) {
            if ($key > 1) {//从第二行开始读取
                if(empty($value['A']) && empty ($value['B']) )
                   continue;
               $data = array(
                   'tm' => trim($value['A']),   //条码
                   'name' => trim($value['B']),   //数量
                   'number' => trim($value['C']),   //数量
                   'lot' => trim($value['D']),   //生产批号
                   'exp' => trim($value['E']),   //截止有效期
                   'enter_price' => trim($value['F']),   //进价
               );
               //不使用对象$Product->name 以免继承使用之前的值
                    //优先取条码名，如果条码为空，则取第二列的产品名称
                $pt =  empty($data['tm']) ? $Product->getByName($data['name']) : $Product->getByTm($data['tm']) ;
                if(empty($pt) )
                  $pt = $Product->getByName($data['name']);

                $name = $pt['name']; 
                if (empty($name)) {
                   //异常记录显示出来，人工去重后再进行插入操作
                  $data['name'] = '<font color="red">EXCEL第  '.$key.'  行异常,请删除</font>';
                }else{
                    $data['name'] = $name;
                }
                //json only filter td,not li ,so use li to stand out
            $data['unit'] = getModelName($Product->unit_id, 'Unit');
            $product_str .= "<tr class='tLeft' id='product_td'>";
            $product_str .="<td>" . $pt['tm'] . '</td>';
            $product_str .="<td><input type='hidden' name='product_id[]' value='" . $Product->id. "'/> <p class='span3'>" . $data['name'] . '</p></td>';
            $product_str .="<td>" . printLensOption($Product->id) . '</td>';
            $product_str .="<td><input type='text' class='input-mini' name='enter_price[]' value='" . $data['enter_price'] . "'/></td>";
            $product_str .="<td><input type='text' class='input-mini' name='number[]' class ='small' value='".$data['number']."' /></td>";    //数量
            $product_str .="<td class='span1'>" . $data['unit'] . "</td>";    //单位
            $product_str .="<td class='span1'>" . getStock($Product->id,1) . "</td>";    //库存,暂时默认青云路仓库TODO
            $product_str .="<td><input type='text' class='input-small' name='lot[]' value='".$data['lot']."' /></td>";   //产品批号
            $product_str .="<td><input type='text' class='input-small' name='exp[]' onClick='WdatePicker()' value='".$data['exp']."' /></td>";   //有效期
            
           $product_str .=  " <td class='span1'><img src='".__APP__."/Public/images/publish_x.png' alt='删除' onclick='removerow(this);'/>".$i."</td></tr>";
            $data = NULL;
            $i++;
            }
        }
        echo "{";
        echo                "error: '" . $error . "',\n";
        echo                "msg: '" . urlencode(($product_str)) . "'";
        echo "}";
       
    }
    
}
?>
